package com.fit.pages.language;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.annotations.AdminAccess;
import com.fit.annotations.AnonymousAccess;
import com.fit.annotations.ProfessorAccess;
import com.fit.annotations.StudentAccess;
import com.fit.dao.language.LanguageDAO;
import com.fit.dao.user.UserDAO;
import com.fit.entities.language.Language;
import com.fit.entities.user.User;
import com.fit.model.MD5;
import com.fit.pages.ErrorPage;

@AdminAccess
@StudentAccess
@AnonymousAccess
@ProfessorAccess
public class Edit {

	@Property
	@Persist
	private Language language;
	@Inject
	private LanguageDAO languageDAO;
	@InjectPage
	private ErrorPage errorPage;
	@Property
	private String newName;

	Object onActivate(Long id) {
		language = languageDAO.findByID(id);
		if (language == null) {
			errorPage.setMessage("Error while trying to view language! ");
			return errorPage;
		}
		return null;
	}

	Object onSuccess() {
		try {

			if (newName != null) {
				newName = newName;
				if (newName.equals(language.getName())) {
					language.setName(newName);
				}
			}
			languageDAO.update(language);
			return Browse.class;
		} catch (Exception e) {
			errorPage.setMessage("Erro while trying to delete language ");
			return errorPage;
		}

	}

}
